$(function() {
	init();
	var data_list = {};
	function getData() {
		var $liEles = $("#data_body ul>li");
		for (let i = 0; i < $liEles.length; i++) {
			let cCls = $liEles.eq(i).find(".data-cell");
			let columnArr = [];
			for (let j = 0; j < cCls.length; j++) {
				if($(cCls[j]).hasClass("choosed_cell")){
					columnArr.push($(cCls[j]).index());
				}
			}
			if(columnArr.length > 0){
				data_list[i+1] = columnArr;
			}
		}
		return data_list;
	}

	function setData(data) {
		// 先清空数据
		initPage();
		data_list = data;
		for(var key in data_list) {
			var $data_body = $("#data_body ul>li").eq(parseInt(key) - 1);
			var data_list_aa = data_list[key];
			for(var i = 0; i < data_list[key].length; i++) {
				$data_body.find("li").eq(data_list[key][i]).addClass("choosed_cell");
			}
		}
	}

	function init() {
		var ele = "<div id='data_top'><dl><dd></dd><dt>布防</dt></dl><dl><dd></dd><dt>撤防</dt></dl></div>"
		ele += "<div id='data_body'>";
		ele += "<div id='data_body_top'>";
		$("#data_body_top").html("<p>" + i + ":00</p>");
		for(var i = 0; i <= 24; i++) {
			ele += "<p>" + i + ":00</p>";
			i++;
		}
		ele += "</div>";
		ele += "<ul>";
		var keep = ["一", "二", "三", "四", "五", "六", "日"];
		for(var i = 0; i < keep.length; i++) {
			ele += "<li><p>" + keep[i] + "</p><ol>";
			for(var j = 0; j < 24; j++) {
				ele += "<li></li>";
			}
			ele = ele + "</ol></li>";
		}
		ele += "</div></div>";
		$("#data").html(ele);
	}
	function initPage() {
		let point = null;
		data_list = {};
		$("#data_body ul>li ol>li").each(function (){
			$(this).removeClass("choosed_cell")
			$(this).addClass("data-cell");
		});
		function fncenter() {
			$(this).toggleClass("choosed_cell");
			// if (this.className === 'data-cell choosed_cell') {
			// 	$(this).removeClass('choosed_cell');
			// } else {
			// 	$(this).addClass('choosed_cell');
			// }
			$('.data-cell').on('mouseenter',(e)=>{
				// if (e.target.className === 'data-cell choosed_cell') {
				// 	$(e.target).removeClass('choosed_cell');
				// } else {
				// 	$(e.target).addClass('choosed_cell');
				// }
				$(e.target).toggleClass('choosed_cell');

				if ($(e.target).hasClass('choosed_cell')){
					point = $(e.target);
				} else {
					if(point){
						point.removeClass("choosed_cell");
						point = null;
					}
				}
			});
		}

		$('.data-cell').on('mousedown',fncenter);
		$('.data-cell').on('mouseup',()=>{
			$('.data-cell').unbind('mouseenter');
		});
	}

	window.getData = getData;
	window.setData = setData;
	window.initPage = initPage;

});